﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using RitS.TestObjects;
using RitS.Graphics;
using RitS.Physics;
using System.Timers;

namespace RitS
{

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static List<IPhysicsBody> gravityBodies = new List<IPhysicsBody>();
        
        Timer GameLoopTimer;

        double Scale = .001;
        Point Origin = new Point(0, 0);
        Point MousePosition = new Point(0, 0);

        int lastTime = 0;

        // Distances in km
        // Mass in Mg (10^3 kg)
        // Velocity in Mm (10^3 m)
        // Time in 100000s

        public MainWindow()
        {
            InitializeComponent();

            SimpleRock Earth = new SimpleRock();
            SimpleRock Moon = new SimpleRock();
            SimpleRock Asteroid = new SimpleRock();

            Earth.Radius = 40000;
            Earth.Mass = 6 * Math.Pow(10, 21);
            Earth.Position = new Point(1000000, 1000000);
            Earth.Velocity = new Vector(0, 0);
            Earth.RelativeCenterMass = new Point(40000, 40000);

            Moon.Radius = 1737;
            Moon.Mass = .07 * Math.Pow(10, 21);
            Moon.Position = new Point(1000000, 1380000);
            Moon.Velocity = new Vector(100, 0);
            Moon.RelativeCenterMass = new Point(2.7, 2.7);

            gravityBodies.Add(Earth);
            gravityBodies.Add(Moon);

            Origin = new Point(620000, 620000);

            GravityPhysicsTools.ExecutePhysicsLoop();

            GameLoopTimer = new Timer(10);
            GameLoopTimer.Elapsed += new ElapsedEventHandler(GameLoopTimer_Elapsed);
            GameLoopTimer.AutoReset = true;
            GameLoopTimer.Enabled = true;
        }

        void GameLoopTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (Dispatcher.Thread != System.Threading.Thread.CurrentThread)
            {
                Dispatcher.BeginInvoke((Action)(delegate { GameLoopTimer_Elapsed(sender, e); }));
            }
            else
            {
             
               // GravityPhysicsTools.ApplyGravityTick(ref gravityBodies, ref gravityBodies);
                List<List<ColoredGeometry>> drawing = new List<List<ColoredGeometry>>();

                foreach (IPhysicsBody body in gravityBodies)
                {
                    drawing.Add(((IDrawable)body).Shape);
                }

                Image finalImage = GraphicsDrawTools.GenerateCanvas(drawing, new Rect(new Size(DrawingCanvas.ActualWidth, DrawingCanvas.ActualHeight)), Scale, Origin);
                DrawingCanvas.Children.Clear();
                DrawingCanvas.Children.Add(finalImage);

                int newTime = Math.Abs(e.SignalTime.Millisecond - lastTime);

                this.Title = newTime.ToString();

                lastTime = e.SignalTime.Millisecond;
            }
        }

        private void DrawingCanvas_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            Scale *= Math.Pow(1.1, e.Delta/50); ;
        }

        private void DrawingCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            Point newMousePosition = e.GetPosition((IInputElement)sender);

            if (e.MiddleButton == MouseButtonState.Pressed)
            {
                Point Delta = new Point(MousePosition.X - newMousePosition.X, MousePosition.Y - newMousePosition.Y);

                Delta.X *= 1 / Scale;
                Delta.Y *= -1 / Scale;

                Origin.X += Delta.X;
                Origin.Y += Delta.Y;
            }

            MousePosition = newMousePosition;
        }
    }
}
